<template>
  <center>
    <h2>新增实验步骤</h2>
  </center>
  <center>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" style="width: 500px">
    <el-form-item label="步骤编号" prop="stepno">
      <el-input v-model="ruleForm.stepno" readonly="true"></el-input>
    </el-form-item>
    <el-form-item label="步骤名称" prop="stepname">
      <el-input v-model="ruleForm.stepname"></el-input>
    </el-form-item>
    <el-form-item label="步骤描述" prop="stepmess">
      <el-input v-model="ruleForm.stepmess"></el-input>
    </el-form-item>
  </el-form>
  </center>

  <center>
    <el-form-item>
      <el-button type="primary" @click="onSubmit('ruleForm')">新建步骤</el-button>
      <el-button type="danger" @click="onCancel()">取消</el-button>
    </el-form-item>
  </center>
</template>

<script>
import {get} from "@/utils/http";
import {ElMessage} from "element-plus";

export default {
  name: "AddStep",
  data() {
    return {
      ruleForm: {
        stepno: '',
        stepname: '',
        stepmess: '',
      },
      rules: {
        stepno:[
          { required: true, message: '请输入步骤编号', trigger: 'blur' }
        ],
        stepname:[
          { required: true, message: '请输入步骤名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        stepmess:[
          { required: true, message: '请输入步骤描述', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  created(){
    this.getOptions();
  },
  methods: {
    getOptions(){
      let that = this;
      get('/getaddstepnojs', {
      }).then(function (res) {
        if (res.code === 1) {
          that.ruleForm.stepno = 0;
          for(var i=0;i<res.data.length;i++){
            if(res.data[res.data.length-1].stepno > that.ruleForm.stepno){
              that.ruleForm.stepno = res.data[res.data.length-1].stepno;
            }
          }
          that.ruleForm.stepno = that.ruleForm.stepno + 1;
          // that.ruleForm.stepnoList.push({value: res.data[i].unitno, label: res.data[i].unitno});
          // that.ruleForm.stepnoList = [{value: res.data[i].stepno, label: res.data[i].stepno}];

          ElMessage.success({
            showClose: true,
            message: "查询成功",
            type: "success",
            center: true
          })
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    onSubmit(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          get('/addstepjs', {
            stepno: that.ruleForm.stepno,
            stepname: that.ruleForm.stepname,
            stepmess: that.ruleForm.stepmess
          }).then(function (res) {
            if (res.code === 1) {
              let router_target = "AddUnit";
              that.$router.push({
                name: router_target,
                params: {
                  stepno: that.ruleForm.stepno
                }
              })

              ElMessage.success({
                showClose: true,
                message: "新建成功",
                type: "success",
                center: true
              })
            } else {
              ElMessage.warning({
                showClose: true,
                message: "新建失败",
                type: "warning",
                center: true
              })
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onCancel(){
      let that = this;
      let router_target = "MainPage";
      that.$router.push({
        name: router_target
      })
    }
  }
}
</script>

<style scoped>

</style>


